/*
 * author      : dormi330
 * date        : 2018/6/26
 * project     : mybatis
 * description : 应用层服务
 */

package com.deep.trade.application;

import com.deep.trade.adaptor.required.member.Member;
import com.deep.trade.adaptor.required.member.MemberService;
import com.deep.trade.application.order.*;
import com.deep.trade.common.CommonUtils;
import com.deep.trade.order.OrderFacade;
import com.deep.trade.order.OrderQueryFacade;
import com.deep.trade.order.dto.*;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderFacadeImpl implements OrderFacade, OrderQueryFacade {
    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    private PlaceOrderService placeOrderService;
    private OrderQueryService orderQueryService;
    private AuditOrderService auditOrderService;
    private CancelOrderService cancelOrderService;
    private OrderDeleteService orderDeleteService;
    private PayOrderService payOrderService;
    private MemberService memberService;
    private AddParcelService addParcelService;
    private ConfirmReceiptService confirmReceiptService;


    @Autowired
    public OrderFacadeImpl(
            PlaceOrderService placeOrderService
            , OrderQueryService orderQueryService
            , AuditOrderService auditOrderService
            , CancelOrderService cancelOrderService
            , OrderDeleteService orderDeleteService
            , PayOrderService payOrderService
            , MemberService memberService
            , AddParcelService addParcelService
            , ConfirmReceiptService confirmReceiptService
    ) {
        this.placeOrderService = placeOrderService;
        this.orderQueryService = orderQueryService;
        this.auditOrderService = auditOrderService;
        this.cancelOrderService = cancelOrderService;
        this.orderDeleteService = orderDeleteService;
        this.payOrderService = payOrderService;
        this.memberService = memberService;
        this.addParcelService = addParcelService;
        this.confirmReceiptService = confirmReceiptService;
    }

    //========================================== OrderFacade ==========================================//
    @Override
    public String placeOrder(InDtoPlaceOrder inDtoPlaceOrder) throws Exception {
        logger.info("{}, {}", CommonUtils.currentClassAndMethod(), inDtoPlaceOrder);
        return placeOrderService.placeOrder(inDtoPlaceOrder);
    }

    @Override
    public void auditTradeOrder(InDtoAuditOrder inDtoAuditOrder) throws Exception {
        auditOrderService.auditTradeOrder(inDtoAuditOrder);
    }

    @Override
    public void cancelTradeOrder(InDtoCancelOrder inDtoCancelOrder) throws Exception {
        cancelOrderService.cancelTradeOrder(inDtoCancelOrder);
    }

    public void deleteTradeOrder(InDtoCancelOrder inDtoCancelOrder) throws Exception {
        orderDeleteService.deleteTradeOrder(inDtoCancelOrder);
    }

    @Override
    public void payOrderSummary(InDtoPayOrderSummary inDtoPayOrderSummary) throws Exception {
        payOrderService.payOrderSummary(inDtoPayOrderSummary);
    }

    @Override
    public void payTradeOrder(InDtoPayTradeOrder inDtoPayTradeOrder) throws Exception {
        payOrderService.payTradeOrder(inDtoPayTradeOrder);
    }

    @Override
    public void addParcel(List<InDtoEachParcel> inDtoEachParcelList) throws Exception {
        addParcelService.addParcel(inDtoEachParcelList);
    }

    @Override
    public void orderConfirmationOfReceipt(String tradeOrderId) throws Exception {
        confirmReceiptService.confirmReceipt(tradeOrderId);
    }

    //========================================== OrderQueryFacade ==========================================//

    public OutDtoCalculatePayment calculatePayment(InDtoPlaceOrder inDtoPlaceOrder) throws Exception {
        return placeOrderService.calculatePayment(inDtoPlaceOrder);
    }

    @Override
    public PageInfo<OutDtoOrderSummary> orderSummaryOfMember(int pageNum, int pageSize) throws Exception {
        Member member = memberService.memberOfToken();
        return orderQueryService.orderSummaryOfMember(member, pageNum, pageSize);
    }

    /**
     * 获取用户的订单列表
     */
    @Override
    public PageInfo<OutDtoTradeOrder> tradeOrderListOfMember(int pageNum, int pageSize) throws Exception {
        Member member = memberService.memberOfToken();
        return orderQueryService.tradeOrderListOfMember(member, pageNum, pageSize);
    }

    /**
     * 获取用户的订单列表, 订单包含 sku信息
     */
    @Override
    public PageInfo<OutDtoTradeOrderWithSku> tradeOrderWithSkuListOfMember(int pageNum, int pageSize) throws Exception {
        Member member = memberService.memberOfToken();
        return orderQueryService.tradeOrderWithSkuListOfMember(member, pageNum, pageSize);
    }

    /**
     * 分页获取 某个租户对所有 订单
     */
    @Override
    public PageInfo<OutDtoTradeOrder> tradeOrderListOfTenant(int pageNum, int pageSize) throws Exception {
        return orderQueryService.tradeOrderListOfTenant(pageNum, pageSize);
    }

    @Override
    public OutDtoOrderDetail tradeOrderDetail(String tradeOrderId) throws Exception {
        return orderQueryService.tradeOrderDetail(tradeOrderId);
    }

    //========================================== OrderQueryFacade ==========================================//

}
